package demo.authentication.server;

import java.net.URL;

import com.google.appengine.api.users.User;
import com.google.gdata.client.calendar.CalendarService;
import com.google.gdata.data.calendar.CalendarEntry;
import com.google.gdata.data.calendar.CalendarFeed;
import com.google.gwt.user.server.rpc.RemoteServiceServlet;

import demo.authentication.client.CalendarConnectionService;
import demo.authentication.client.CalendarData;

public class CalendarConnectionServiceImpl extends RemoteServiceServlet
		implements CalendarConnectionService {

	public CalendarData getConnection(String token) {
		CalendarData data = new CalendarData();
        User user = null;

		try {
			
//			OAuthService oauth = OAuthServiceFactory.getOAuthService();
//			user = oauth.getCurrentUser();

			
//			String CONSUMER_KEY = "rexborn.com";
//			String CONSUMER_SECRET = "l+0rv4X5BhLAZs7SdvJRz1I5";
//
//			GoogleOAuthParameters oauthParameters = new GoogleOAuthParameters();
//			oauthParameters.setOAuthConsumerKey(CONSUMER_KEY);
//			oauthParameters.setOAuthConsumerSecret(CONSUMER_SECRET);
//			oauthParameters.setScope("https://docs.google.com/feeds/");
//			oauthParameters.setOAuthCallback("http://www.example.com/UpgradeToken.jsp");
//
//			GoogleOAuthHelper oauthHelper = new GoogleOAuthHelper(new OAuthHmacSha1Signer());
//			oauthHelper.getUnauthorizedRequestToken(oauthParameters);
//			
//			
//			
			CalendarService myService = new CalendarService(
					"exampleCo-exampleApp-1");
			myService.setUserToken(token);
		//	myService.setUserCredentials("karel@rexborn.com", "capgemini");
			// myService.setUserCredentials("karel.vanlaer@gmail.com",
			// "charly17");
			URL feedUrl = new URL(
					"https://www.google.com/calendar/feeds/default/allcalendars/full");
			CalendarFeed resultFeed = myService.getFeed(feedUrl,
					CalendarFeed.class);
			String result = "Your calendars:\n";
			data.setName("calendars");
//			data.setCalendars(new Vector<String>());
//			data.setEntries(new Vector<String>());
			for (int i = 0; i < resultFeed.getEntries().size(); i++) {
				CalendarEntry entry = resultFeed.getEntries().get(i);

				data.getCalendars().add(entry.getTitle().getPlainText());
				// result += "\t" + entry.getTitle().getPlainText() + ":"
				// + entry.getAccessLevel() + "\n";

//				Link link = entry.getLink(
//						AclNamespace.LINK_REL_ACCESS_CONTROL_LIST,
//						Link.Type.ATOM);

				// For each calendar, retrieve its ACL feed.
//				if (link != null) {
//
//					AclFeed aclFeed = myService.getFeed(
//							new URL(link.getHref()), AclFeed.class);
//					for (AclEntry aclEntry : aclFeed.getEntries()) {
//						data.getEntries().add(
//								aclEntry.getTitle().getPlainText());
//						aclEntry.getContent().result += "\t\tScope: Type="
//								+ aclEntry.getScope().getType() + " ("
//								+ aclEntry.getScope().getValue() + ")\n";
//						result += "\t\tRole: " + aclEntry.getRole().getValue();
//					}
//				}
			}
			return data;
		} catch (Exception e) {
			data.setName("exception:" + e.toString());
			return data;
		}

	}

}
